// wilddog.initializeApp({syncURL: "https://"+sessionStorage.database+".wilddogio.com"});
// var REF= wilddog.sync().ref();
var config = {
    syncURL: "https://wd3338676115mpoocn.wilddogio.com" //输入节点 URL
};
wilddog.initializeApp(config);
var REF = wilddog.sync().ref();
function initBD(data) {
    REF.set(data);
}

function data_save(url,arry) {
    var postsRef = REF.child(url);
    for (var i = 0; i < arry.length; i++) {
        postsRef.push(arry[i]);
    }
}

function data_delete(options) {
    var postsRef = REF.child(options.url);
    postsRef.remove()
        .then(function(){
            options.success();
            console.info('remove node success.')
        })
        .catch(function(err){
            options.fail();
            console.info('remove node failed', err.code, err);
        });
}

function data_getAll(callback) {
    REF.once("value", function(snapshot) {
        callback(snapshot.val());
    });
}

function data_getBy(option) {
    var postsRef = REF.child(option.url);
    postsRef.orderByChild(option.key).startAt(option.value).limitToFirst(option.datalength).once("value", function(snapshot) {
        option.success(snapshot.val());
    });
}

function data_getAllFrom(url,callback) {
    var postsRef = REF.child(url);
    postsRef.once("value", function(snapshot) {
        callback(snapshot.val());
    });
}

function data_updata(options) {
    var postsRef = REF.child(options.url);
    postsRef.update(options.data)
        .then(function(){
            options.success();
            console.info('update data success.')
        })
        .catch(function(err){
            options.fail();
            console.info('update data failed', err.code, err);
        });
}

